package com.example.content_android.ui.home;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.content_android.GlideImageLoader;
import com.example.content_android.R;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class HomeFragment extends Fragment implements AnnouncementFragment.OnFragmentInteractionListener, ArticleListFragment.OnFragmentInteractionListener {
    List<String> bannerList = new ArrayList<>();
    private HomeViewModel homeViewModel;
    private ViewPager viewPagers;
    private TabLayout tabLayout;
    private List<Fragment> fragments = new ArrayList<>();
    private List<String> tabName = new ArrayList<>(Arrays.asList("最新公告", "最新文章"));

    @Override
    public void onFragmentInteraction(Uri uri) {
        System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");

    }

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
//        initView();


        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        viewPagers = root.findViewById(R.id.viewPagers);
//                (ViewPager) getView().findViewById();
        tabLayout =  root.findViewById(R.id.tab_layout);
//                (TabLayout) getView().findViewById(R.id.tab_layout);
        initData();
//        final TextView textView = root.findViewById(R.id.text_home);
//        homeViewModel.getText().observe(this, new Observer<String>() {
//            @Override
//            public void onChanged(@Nullable String s) {
//                textView.setText(s);
//            }
//        });
        String url = "https://www.turkeylike.cn:8900/fzulab/indexPic/listall";
        OkHttpUtils
                .get()
                .url(url)
//                .addParams("username", "hyman")
//                .addParams("password", "123")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("onResponse", "response");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        JSONObject responseStr = JSON.parseObject(response);

                        Map<String, String> map2 = JSON.parseObject(response, Map.class);

                        JSONArray bannerArray = responseStr.getJSONArray("result");
                        if (bannerArray != null && bannerArray.size() > 0) {
                            for (int i = 0; i < bannerArray.size(); i++) {
                                JSONObject str = JSON.parseObject(bannerArray.get(i).toString());
                                bannerList.add(str.get("imgUrl").toString());

                            }
                        }
                        Banner banner = (Banner) getView().findViewById(R.id.home_banner);
                        //设置图片加载器
                        banner.setImageLoader(new GlideImageLoader());
                        //设置图片集合
                        banner.setImages(bannerList);
                        //banner设置方法全部调用完毕时最后调用
                        banner.start();

                        Log.e("onResponse", "response");
                    }
                });
        return root;
    }

    private void initData() {
        //设置数据
        for (String s : tabName) {
            tabLayout.addTab(tabLayout.newTab().setText(s));
        }
        AnnouncementFragment announcementFragment = new AnnouncementFragment();
        ArticleListFragment articleListFragment = new ArticleListFragment();
        fragments.add(announcementFragment);
        fragments.add(articleListFragment);
        ViewPageAdata viewPageAdata = new ViewPageAdata(getActivity().getSupportFragmentManager());
        viewPageAdata.setmFragments(fragments);
        viewPagers.setAdapter(viewPageAdata);
//        tabLayout.setupWithViewPager(viewPagers);
        //关联tablayout 和viewpager
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPagers));
        viewPagers.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                Log.e("onPageSelected: ", i + "");
                tabLayout.getTabAt(i).select();

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });


    }

    private void initView() {
        View vv = getView();
        Activity act = getActivity();
        getParentFragment();
        getTargetFragment();
        getChildFragmentManager();
        this.getView().getRootView();

    }

    class ViewPageAdata extends FragmentStatePagerAdapter {


        List<Fragment> mFragments;


        public ViewPageAdata(FragmentManager fm) {
            super(fm);
        }

        public void setmFragments(List<Fragment> mFragments) {
            this.mFragments = mFragments;
        }

        @Override
        public Fragment getItem(int i) {
            return mFragments.get(i);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}